package org.springframework.prospring.ticket.service.impl;

import org.springframework.prospring.ticket.service.ClubManager;
import org.springframework.prospring.ticket.service.CityManager;
import org.springframework.prospring.ticket.dao.ClubDAO;
import org.springframework.prospring.ticket.dao.CityDAO;
import org.springframework.prospring.ticket.domain.Club;
import org.springframework.prospring.ticket.domain.Field;
import org.springframework.prospring.ticket.domain.Turn;
import org.springframework.prospring.ticket.domain.location.City;
import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.domain.user.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Collection;
import java.util.Date;
import java.util.ArrayList;
import java.sql.Timestamp;

public class CityManagerImpl implements CityManager {
    private static Log logger = LogFactory.getLog(CityManagerImpl.class);
    private CityDAO dao;

    public CityManagerImpl() {
        super();
        if (logger.isDebugEnabled()) {
            logger.debug("in CityManagerImpl Constructor");
        }
    }

    public void setCityDAO(CityDAO dao) {
        if (logger.isDebugEnabled()) {
            logger.debug("setting CityDAO: " + dao);
        }

        this.dao = dao;
    }

    public City findCity(Long cityId) {

        if (logger.isDebugEnabled()) {
            logger.debug("finding City id: "+cityId);
        }
        City city = dao.findCity(cityId);

        if (city == null) {
            logger.warn("CityId '" + cityId + "' not found in database.");
        }

        return city;
    }

    public Collection<City> findAllCities(){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Cities ");
        }

        Collection<City> cities = dao.findAllCities();

        if (cities == null) {
            logger.warn("Not found any Cities in database.");
        }

        return cities;
    }

    public Collection <Region> findRegionsByCity(Long cityId){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Regions by City id="+cityId);
        }

        Collection<Region> regions = dao.findRegionsByCity(cityId);

        if (regions == null) {
            logger.warn("Not found any Regions in database for City id="+cityId);
        }
        Collection regionsForView = new ArrayList();
        if (regions == null) {
            logger.warn("Not Found Regions for cityId '" + cityId + "',in database.");
        } else {
            for (Region region: regions){
                Region regionForView = new Region();
                regionForView = region;
                regionsForView.add(regionForView);
            }
        }

        return regionsForView;
    }

    public Collection<City> findAllCitiesByProvinceId(Long provinceId){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Cities by province id="+provinceId);
        }

        Collection<City> cities = dao.findAllCities();

        if (cities == null) {
            logger.warn("Not found any Cities in database for province id="+provinceId);
        }

        return cities;
    }




    public City saveCity(City city) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("saving City: " + city);
        }

        dao.saveCity(city);

        return city;
    }

}
